--[[------------------------------------------------------------------------------------------------ Helicopter combat Андрущенко Иван --------------------------------------------------------------------------------------------------]] local heli_firer = {} function get_heli_firer(obj) if heli_firer[obj:id()] == nil then heli_firer[obj:id()] = heli_fire(obj) end return heli_firer[obj:id()] end class "heli_fire" function heli_fire:__init(obj) self.obj = obj self.enemy_ = nil self.fire_point = nil self.enemy_id = nil self.flag_by_enemy = true self.hit_count = 0 self.fire_id = nil self.enumy_die = true self.enemy_time = time_global() self.upd_vis = 0 self.show_health = false end function heli_fire:update_enemy_state() local heli = self.obj:get_helicopter() local enemy = self.enemy_id and db.storage[self.enemy_id] and db.storage[self.enemy_id].object --' printf("update_enemy_state()") if self.hit_count > 2 then self.hit_count = 0 self.flag_by_enemy = true end if enemy and self.enemy_die and self.enemy_ == "all" then self:update_enemy_arr() end if enemy and time_global() - self.enemy_time > self.upd_vis * 1000 then --'printf("self.upd_vis = %d", self.upd_vis); if not heli:isVisible( enemy )then if self.enemy_ == "all" then self:update_enemy_arr() end end self.enemy_time = time_global() end if enemy then if not heli:isVisible( enemy )then self.flag_by_enemy = true end end self:set_enemy() end function heli_fire:set_enemy() local enemy = self.enemy_id and db.storage[self.enemy_id] and db.storage[self.enemy_id].object local heli = self.obj:get_helicopter() if self.flag_by_enemy then heli:ClearEnemy() --'printf("ClearEnemy()") self.enemy_die = false if enemy then if heli:isVisible( enemy ) then heli:SetEnemy( enemy) self.flag_by_enemy = false end else if self.enemy_ then if self.enemy_ == "actor" then if db.actor then enemy = db.actor end else if self.enemy_ == "all" then self:update_enemy_arr() else if self.enemy_ ~= "nil" then enemy = get_story_object(self.enemy_) self.enemy_id = enemy and enemy:id() end end end end if enemy then heli:SetEnemy(enemy) --'printf("set_enemy(enemy, actor or SID)") else self.enemy_die = true end self.flag_by_enemy = false end end if (self.enemy_die) then if (self.fire_point) then heli:SetEnemy(self.fire_point) end elseif (enemy and enemy:death_time() > 0) then heli:ClearEnemy() self.enemy_die = true end end function heli_fire:update_enemy_arr() --'printf("update_enemy_arr()") local heli = self.obj:get_helicopter() local index = 0 local min_dist2D = 65000 --' printf("heli_enemy_count=%d", db.heli_enemy_count) local size = #db.heli_enemies while index < size do local id = db.heli_enemies[index] local enemy = id and db.storage[id] and db.storage[id].object if enemy then if heli:isVisible(enemy) then if distance_2d( self.obj:position(), enemy:position() ) < min_dist2D then self.enemy_id = enemy:id() min_dist2D = distance_2d( self.obj:position(), enemy:position() ) self.flag_by_enemy = true end end end index = index + 1 end if heli:isVisible( db.actor ) and random_choice( false, true) or size==0 then if distance_2d( self.obj:position(), db.actor:position() ) <= min_dist2D*2 then self.enemy_id = AC_ID min_dist2D = distance_2d( self.obj:position(), db.actor:position() ) end end end --[[ Disabled in favor of ui_enemy_health. If you want to allow heli to have health bar simply set db.storage[heli:id()].show_health = true function heli_fire:update_hit() if self.show_health then self:set_cs_heli_progress_health() else self:cs_remove() end if self.enemy:id() == self.fire_id then if self.enemy_ ~= "nil" then self.hit_count = self.hit_count + 1 else self.hit_count = 0 end else self.fire_id = self.enemy:id() self.hit_count = 1 end end function heli_fire:cs_heli() local hud = get_hud() local custom_static = hud:GetCustomStatic("cs_heli_health") if custom_static == nil then hud:AddCustomStatic("cs_heli_health", true) local xml = CScriptXmlInit() xml:ParseFile("heli_progress.xml") local st = hud:GetCustomStatic("cs_heli_health") local w = st:wnd() self.heli_progress = xml:InitProgressBar ("heli_health", w ) self:set_cs_heli_progress_health() end end function heli_fire:set_cs_heli_progress_health() local heli = self.obj:get_helicopter() local hud = get_hud() local custom_static = hud:GetCustomStatic("cs_heli_health") local xml = CScriptXmlInit() xml:ParseFile("heli_progress.xml") if custom_static then hud:AddCustomStatic("cs_heli_health", true) local st = hud:GetCustomStatic("cs_heli_health") local w = st:wnd() local _progr = heli:GetfHealth()*100 if _progr > 0 then self.heli_progress:Show(true) self.heli_progress:SetProgressPos(_progr) else self.heli_progress:Show(false) self.show_healt = false hud:RemoveCustomStatic("cs_heli_health") end end end function heli_fire:cs_remove() local hud = get_hud() local custom_static = hud:GetCustomStatic("cs_heli_health") if custom_static then hud:RemoveCustomStatic("cs_heli_health") end end --]]